package com.ywjh.inter.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ywjh.inter.entity.InsulinPenInjectSetEntity;

public class InsulinPenInjectSetDAO {

	public boolean insert(Connection conn, InsulinPenInjectSetEntity entity) throws SQLException {
		PreparedStatement ps = conn.prepareStatement(
			"insert into insulin_pen_inject_set(sn,chn,drug_id,am_vlu,noon_vlu,pm_vlu,base_vlu,temp_vlu,drug_id2,user_id,doc_id,type,isnew,issued,create_time)"
			+ "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
		ps.setString(1, entity.getSn());
		ps.setString(2, entity.getChn());
		ps.setString(3, entity.getDrugId());
		ps.setString(4, entity.getAmVlu());
		ps.setString(5, entity.getNoonVlu());
		ps.setString(6, entity.getPmVlu());
		ps.setString(7, entity.getBaseVlu());
		ps.setString(8, entity.getTempVlu());		
		ps.setString(9, entity.getDrugId2());
		ps.setInt(10, entity.getUserId());
		ps.setInt(11, entity.getDocId());
		ps.setInt(12, entity.getType());
		ps.setInt(13, entity.getIsnew());
		ps.setInt(14, entity.getIssued());
		ps.setTimestamp(15, new Timestamp(new Date().getTime()));

		boolean r = false;
		try {
			r = ps.executeUpdate() > 0;
		} catch(Exception e) {
			e.printStackTrace();
		}
		return r;
	}
	
	public boolean updateIssued(Connection conn, int id) throws SQLException {
		PreparedStatement ps = conn.prepareStatement("update insulin_pen_inject_set set issued=1 where id=?");
		ps.setInt(1, id);

		boolean r = false;
		try {
			r = ps.executeUpdate() > 0;
		} catch(Exception e) {
			e.printStackTrace();
		}
		return r;
	}
	
	/**
	 * 获取医嘱
	 * @param conn
	 * @param userId
	 * @param page
	 * @param num
	 * @return
	 * @throws SQLException
	 */
	public List<InsulinPenInjectSetEntity> getDocSet(Connection conn, int userId, int page, int num) throws SQLException {
		List<InsulinPenInjectSetEntity> r = new ArrayList<>();
		PreparedStatement ps = conn.prepareStatement("select s.*,e.name as equ_name,p.name as doc_name "
				+ "from insulin_pen_inject_set s "
				+ "join equ_bind e on e.user_id=s.user_id and e.sn=s.sn and e.type=6 and e.status=1 "
				+ "join doc_profile p on p.user_id=s.doc_id "
				+ "where s.user_id=? and s.type=2 order by s.create_time desc limit ?,?");
		ps.setInt(1, userId);
		ps.setInt(2, (page - 1) * num);
		ps.setInt(3, num);
		ResultSet rs = ps.executeQuery();
		while(rs.next()) {
			InsulinPenInjectSetEntity a = new InsulinPenInjectSetEntity();
			a.setSn(rs.getString("sn"));
			a.setUserId(rs.getInt("user_id"));
			a.setDrugId(rs.getString("drug_id"));
			a.setAmVlu(rs.getString("am_vlu"));
			a.setNoonVlu(rs.getString("noon_vlu"));
			a.setPmVlu(rs.getString("pm_vlu"));
			a.setBaseVlu(rs.getString("base_vlu"));
			a.setTempVlu(rs.getString("temp_vlu"));
			a.setEquName(rs.getString("equ_name"));
			a.setDocId(rs.getInt("doc_id"));
			a.setDocName(rs.getString("doc_name"));
			a.setType(rs.getInt("type"));
			a.setIsnew(rs.getInt("isnew"));
			a.setIssued(rs.getInt("issued"));
			a.setCreateTime(rs.getString("create_time"));
			r.add(a);;
		}
		return r;
	}
	
	public int getDocSetCount(Connection conn, int userId) throws SQLException {
		int r = 0;
		PreparedStatement ps = conn.prepareStatement("select count(*) as cnt "
				+ "from insulin_pen_inject_set s "
				+ "join equ_bind e on e.user_id=s.user_id and e.sn=s.sn and e.type=6 and e.status=1 "
				+ "join doc_profile p on p.user_id=s.doc_id "
				+ "where s.user_id=? and s.type=2");
		ps.setInt(1, userId);
		ResultSet rs = ps.executeQuery();
		if(rs.next()) {
			r = rs.getInt("cnt");
		}
		return r;
	}
	
	public int getDocSetNewCount(Connection conn, int userId) throws SQLException {
		int r = 0;
		PreparedStatement ps = conn.prepareStatement("select count(*) as cnt "
				+ "from insulin_pen_inject_set "
				+ "where user_id=? and type=2 and isnew=1");
		ps.setInt(1, userId);
		ResultSet rs = ps.executeQuery();
		if(rs.next()) {
			r = rs.getInt("cnt");
		}
		return r;
	}
	
	public boolean setDocSetOld(Connection conn, int userId) throws SQLException {
		boolean r = false;
		PreparedStatement ps = conn.prepareStatement("update insulin_pen_inject_set set isnew=0 where user_id=? and type=2 and isnew=1");
		ps.setInt(1, userId);
		try {
			r = ps.executeUpdate() > 0;
		} catch(Exception e) {
			e.printStackTrace();
		}
		return r;
	}
	
	/**
	 * 获取下发历史
	 * @return
	 */
	public List<InsulinPenInjectSetEntity> getIssueLog(Connection conn, int userId, int page, int num) throws SQLException {
		List<InsulinPenInjectSetEntity> r = new ArrayList<>();
		PreparedStatement ps = conn.prepareStatement("select s.*,e.name as equ_name "
				+ "from insulin_pen_inject_set s "
				+ "join equ_bind e on e.user_id=s.user_id and e.sn=s.sn and e.type=6 and e.status=1 "
				+ "where s.user_id=? and s.issued=1 order by s.create_time desc limit ?,?");
		ps.setInt(1, userId);
		ps.setInt(2, (page - 1) * num);
		ps.setInt(3, num);
		ResultSet rs = ps.executeQuery();
		while(rs.next()) {
			InsulinPenInjectSetEntity a = new InsulinPenInjectSetEntity();
			a.setSn(rs.getString("sn"));
			a.setDrugId(rs.getString("drug_id"));
			a.setAmVlu(rs.getString("am_vlu"));
			a.setNoonVlu(rs.getString("noon_vlu"));
			a.setPmVlu(rs.getString("pm_vlu"));
			a.setBaseVlu(rs.getString("base_vlu"));
			a.setTempVlu(rs.getString("temp_vlu"));
			a.setEquName(rs.getString("equ_name"));
			a.setCreateTime(rs.getString("create_time"));
			r.add(a);
		}
		return r;
	}
	
	public int getIssueLogCount(Connection conn, int userId) throws SQLException {
		int r = 0;
		PreparedStatement ps = conn.prepareStatement("select count(*) as cnt "
				+ "from insulin_pen_inject_set s "
				+ "join equ_bind e on e.user_id=s.user_id and e.sn=s.sn and e.type=6 and e.status=1 "
				+ "where s.user_id=? and s.issued=1");
		ps.setInt(1, userId);
		ResultSet rs = ps.executeQuery();
		if(rs.next()) {
			r = rs.getInt("cnt");
		}
		return r;
	}

}
